package acm.蓝桥杯模拟;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 好困阿 {
    static int n;
    static int m;
    static int[][][] dp;
    static char[][] map;
    public static void main(String[] args) {
        Reader sc = new Reader();
        n = sc.nextInt();
        m = sc.nextInt();
        map=new char[n][];
        dp=new int[n][m][2];    //0存储左上的个数，1存储右上的个数
        for (int i = 0; i < map.length; i++) {
            map[i]=sc.nextLine().toCharArray();
        }
        int res=0;
        for (int i = 1; i < n-1; i++) {
            for (int j = 1; j < m-1; j++) {
                if (map[i][j]==map[i-1][j-1]){
                    dp[i][j][0]=dp[i-1][j-1][0]+1;
                }
                if (map[i][j]==map[i-1][j+1]){
                    dp[i][j][1]=dp[i-1][j+1][1]+1;
                }
                int curr=Math.min(dp[i][j][0],dp[i][j][1]);
                res=Math.max(res,curr);
            }
        }
        System.out.println(res);
    }


    static class Reader {
        private StringTokenizer in=new StringTokenizer("");
        private BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        public String next(){
            hasNext();
            return in.nextToken();
        }
        public boolean hasNext(){
            if (!in.hasMoreTokens()) {
                String line = null;
                try {
                    line = reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (line==null){
                    return false;
                }
                in=new StringTokenizer(line);
            }
            return true;
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public String nextLine(){
            try {
                in=new StringTokenizer("");
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
